//package com.geeklan.smart.data.remote.mqtt;
//
//import android.app.Service;
//import android.content.Intent;
//import android.os.IBinder;
//
//import androidx.lifecycle.MutableLiveData;
//
//import com.geeklan.base.utils.MLog;
//
//import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
//import org.eclipse.paho.client.mqttv3.IMqttMessageListener;
//import org.eclipse.paho.client.mqttv3.MqttCallbackExtended;
//import org.eclipse.paho.client.mqttv3.MqttClient;
//import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
//import org.eclipse.paho.client.mqttv3.MqttException;
//import org.eclipse.paho.client.mqttv3.MqttMessage;
//
//public class MqttService extends Service implements MqttCallbackExtended ,IMqttMessageListener{
//    private static final String TAG = "MqttService";
//    private BaseMqttClient mqttClient;
//    private static final String topic = "topic";
//    private MutableLiveData<MqttMessage> messageLiveData = new MutableLiveData<>();
//    private static MqttService instance;
//
//    public static synchronized MqttService getInstance() {
//        return instance;
//    }
//
//    public MutableLiveData<MqttMessage> getMessageLiveData() {
//        return messageLiveData;
//    }
//
//    @Override
//    public void onCreate() {
//        super.onCreate();
//        instance = this;
//        // 在服务创建时连接到 MQTT 服务器
//        connectToMqttBroker();
//    }
//
//    @Override
//    public int onStartCommand(Intent intent, int flags, int startId) {
//        return START_STICKY;
//    }
//
//    @Override
//    public IBinder onBind(Intent intent) {
//        return null;
//    }
//
//    @Override
//    public void onDestroy() {
//        super.onDestroy();
//        // 释放资源，断开连接
//        disconnect();
//    }
//
//    private void connectToMqttBroker() {
//        try {
//            mqttClient = new BaseMqttClient(MqttClient.generateClientId());
//            mqttClient.setCallback(this);
//            MLog.d(TAG, "Connected to MQTT broker");
//        } catch (MqttException e) {
//            MLog.e(TAG, "Error connecting to MQTT broker" + e);
//        }
//    }
//
//    // 订阅主题
//    public void subscribe(String topic) throws MqttException {
//        mqttClient.subscribe(topic, this);
//    }
//
//    public void sendMessage(String topic, String message) {
//        try {
//            if (mqttClient != null && mqttClient.isConnected()) {
//                MqttMessage mqttMessage = new MqttMessage(message.getBytes());
//                mqttClient.publish(topic, mqttMessage);
//                MLog.d(TAG, "Message sent to topic " + topic + ": " + message);
//            } else {
//                MLog.e(TAG, "MQTT client is not connected");
//            }
//        } catch (MqttException e) {
//            MLog.e(TAG, "Error sending message" + e);
//        }
//    }
//
//
//    // 断开连接
//    private void disconnect() {
//        try {
//            if (mqttClient != null && mqttClient.isConnected()) {
//                mqttClient.disconnect();
//                MLog.d(TAG, "Disconnected from MQTT broker");
//            }
//        } catch (MqttException e) {
//            MLog.e(TAG, "Error disconnecting from MQTT broker" + e);
//        }
//    }
//
//    @Override
//    public void connectComplete(boolean reconnect, String serverURI) {
//        //当连接成功时触发。如果reconnect参数为true，则表示这是一次重新连接；否则是首次连接。
//        MLog.i(TAG, "MQTT connection successful");
//        try {
//            // 可以选择在连接成功后进行订阅操作
//            subscribe(topic);
//        } catch (MqttException e) {
//            MLog.e(TAG, "subscribeTopic fail:\n" + e.getMessage());
//        }
//    }
//
//    @Override
//    public void connectionLost(Throwable cause) {
//        MLog.e(TAG, "MQTT connection disconnected : \n" + cause.getMessage());
//        // 当连接意外中断时调用，比如网络断开。
//    }
//
//    @Override
//    public void messageArrived(String topic, MqttMessage message) throws Exception {
//        // 收到消息时的处理
//        String payload = new String(message.getPayload());
//        MLog.i(TAG, "Received server message : " + topic + "\n " + payload);
//        MqttMessage mqttMessage = new MqttMessage(payload.getBytes());
//        messageLiveData.postValue(mqttMessage); // 将消息发送给 LiveData
//    }
//
//    @Override
//    public void deliveryComplete(IMqttDeliveryToken token) {
//        MLog.i(TAG, "Message sent successfully : " + token.getMessageId());
//        // 当消息成功传送后调用。
//    }
//}
